var rootPath = getRootPath();

$(function() {
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	jQuery.validator.addMethod("checkUname", function(value, element) {
		var oriUname = $("#oriUname").val();
		return checkLogin(value,oriUname);
	}, "昵称已存在");
	
	jQuery.validator.addMethod("isRightPhone", function(value, element) {
		return isRightPhone(value);
	}, "手机号码格式有误");
	
	jQuery.validator.addMethod("checkPhone", function(value, element) {
		var oriPhone = $("#oriPhone").val();
		return checkLogin(value,oriPhone);
	}, "手机号码已存在");
	
	jQuery.validator.addMethod("checkEmail", function(value, element) {
		var oriEmail = $("#oriEmail").val();
		return checkLogin(value,oriEmail);
	}, "电子邮箱已存在");
	 
	
	$("#form-user-edit").validate({
		rules:{
			uname:{
				required:true,
				checkUname:true
			},
			phone:{
				isPhone:true,
				isRightPhone : true,
				checkPhone:true
			},
			email:{
				email:true,
				checkEmail:true
			},
			role:{
				required:true,
			},
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			var jsonObject = JSON.parse(JSON.stringify($('#form-user-edit').serializeJson()));
			var json;
			if(jsonObject.rleIds == null) {
				jsonObject.rleIds = [];
				json = JSON.stringify(jsonObject);
			} else {
				json = JSON.stringify($('#form-user-edit').serializeJson());
			}
			$.ajax({
				url: rootPath + '/user/updateUserLogin',
	            type: 'POST',
	            data: json,
	            contentType: 'application/json;charset=UTF-8',
	            success: function (data) {
	            	if("0000" == data.returnCode) {
	            		layer.msg('成功更新User!',{icon:1,time:1000},function() {
//	            			parent.window.location.href = rootPath + '/userPage';
	            			var index = parent.layer.getFrameIndex(window.name);
//	            			parent.$('.btn-refresh').click();
	            			parent.$("#searchUser").click();
	            			parent.layer.close(index);
	            		});	            		
	            	} else {
	            		layer.alert(data.returnMessage);
	            	}
	            },
	            error: function (jqXHR, textStatus, errorThrown) {
	            	layer.msg('error:)',{icon:1,time:1000});
	            }
	        });
		}
	});	
});

/**
 * 检测 login是否有效
 * @param login 填入的login
 * @param oriLogin 原始的login
 * @returns {Boolean}
 */
function checkLogin(login,oriLogin) {
	if(login == '') {
		return true;
	}
	var isValidLogin = true;
	if(login != oriLogin) {
		$.ajax({
			url: rootPath + '/login/checkLogin',
	        type: 'GET',
	        async: false,
	        data: {
	        	"login" : login
	        },
	        success: function (data) {
	        	if(data != "true") {
	        		isValidLogin = false;
	        	}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error:(");
	        }
	    });
	}
	return isValidLogin;
}

function isRightPhone(phone) {
	if(phone == '') {
		return true;
	}
	var reg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
	return reg.test(phone);
}